//精简原则
#include "cc_pub.h"

#define LLOG
#define LLOG_ERR
#define LLOG_MARK
#define LLOG_ASSERT
#include "llog.h"

#ifdef CFG_MEM_API					//  内存处理函数
void cc_memcpy(unsigned char* p2da,unsigned char * p2sa,unsigned int size){
    if(size ==0){
        return;
    }else{
        unsigned int i;
        for(i=0;i<size;i++){
            (*(p2da+i)) = (*(p2sa+i));
        }
    }
}

int cc_memcmp(unsigned char* p2da,unsigned char * p2sa,unsigned int size){
    int ret = CMP_OK;//TRUE
    if(size ==0){
        ret = CMP_ERR;//fault
    }else{
        unsigned int i;
        for(i=0;i<size;i++){
            if( (*(p2da+i)) != (*(p2sa+i))){
                ret=CMP_ERR;
                break;
            }
        }
    }
    return ret;
}

void cc_memset(unsigned char* p2da,unsigned char val,unsigned int size){
    if(size ==0){
        return;
    }else{
        unsigned int i;
        for(i=0;i<size;i++){
            (*(p2da+i)) = val;
        }
    }
}

void cc_memcfb(unsigned char* p2da,unsigned char* p2sa,unsigned int size){
    //反序列 复制
	if(size ==0){
        return;
    }else{
        unsigned int i,j;
        j=size-1;
        for(i=0;i<size;i++){
            *(p2da+i)=*(p2sa+(j--));
        }             
    }
}

#endif

#ifdef CFG_MSTICK_API 				// MsTick  
static tMs (*g_pGetSysTick)(void); //GetTick_Now

void MsTick_Init(tMs (*p2f_gettick)(void)){      //模块初始化
	g_pGetSysTick=p2f_gettick;
}

tMs GetNowTick(void){
	if(g_pGetSysTick == PUB_NULL){
		return 0;
	}else{
		return g_pGetSysTick();	
	}
}

int Check_TimeOut(tMs oldtick,tMs diff_tick){
	tMs now;
	tMs diff;
	
	now = GetNowTick();
	if(now >= oldtick){
		diff = now-oldtick;		
	}
	else{
		diff =(TYPE_MS_MAX_VAL-oldtick)+now;
	}
	
	if(diff > diff_tick){
		return RET_OT;	//超时
	}else{
		return RET_NT; 	//
	}	
}

#endif

#ifdef CFG_TINY_DBG_LOG_API			// 简单 常用 Log API 

#define TLOG_PRINT 		LLOG 

void TinyDbg_Log_Level(uint8_t Level,char* FuncName,uint16_t Line){
	switch(Level){
		case 1:{TLOG_PRINT("#E %s L[%d]\n",FuncName,Line);}break;
		case 2:{TLOG_PRINT("#M %s L[%d]\n",FuncName,Line);}break;
		case 3:{TLOG_PRINT("#W %s L[%d]\n",FuncName,Line);}break;
		case 4:{TLOG_PRINT("#T %s L[%d] T[%d]\n",FuncName,Line,GetNowTick());}break;
	}
}
	
void Log_ShowArray(const uint8_t* pStr,uint16_t Len,uint8_t *pPkg){     

    uint16_t i;
	if(pStr != NULL)
		TLOG_PRINT("#I %s:",pStr); 
	// TLOG_PRINT("#I %s :\n",pStr); 
	for (i=0; i<Len; i++){
		TLOG_PRINT(" 0x%x",pPkg[i]);
	}
    TLOG_PRINT("\r\n");	  
}

void Log_ShowArrayByCol(const uint8_t* pStr,uint8_t ColNum,
	uint16_t ArraySz,uint8_t* pArray){
	
	uint16_t i,cnt;
	
	if(pStr!=NULL){
		TLOG_PRINT("#I %s :\n",pStr); 
	}
	cnt=0;
	for(i=0;i<ArraySz;i++){
		cnt++;
		// TLOG_PRINT(" [%x]",pArray[i]);
		TLOG_PRINT(" 0x%2x",pArray[i]);
		if(cnt == ColNum){
			cnt=0;
			TLOG_PRINT("\n");
		}
	}
	if(cnt != 0){
		TLOG_PRINT("\n");
	}
}

#endif

#ifdef CFG_STATIC_BUF_ALLOCATE		// 静态的空间顺序 分配 

static uint8_t	g_StaticBuf_Space[CFG_SET_SBUF_SZ];
static uint32_t	g_StaticBuf_Cnt=0;
static uint32_t	g_StaticBuf_ReqCnt=0;
#define SBUF_MAX_SIZE	sizeof(g_StaticBuf_Space)

void* CC_StaticBuf_Allocate(uint16_t Sz,uint16_t Line){
	// 1-简单； 2-有利于静态对象创建接口的简化
	uint8_t* ptmp=NULL;
	uint32_t remain;
	uint32_t RealSz;
	
	if(Sz == 0){
		// LOG_ERR();
		LLOG("#E SBUF SZ=0 L[%d]\n",Line);		
		return ptmp;
	}
	remain=SBUF_MAX_SIZE-g_StaticBuf_Cnt;
	RealSz=0;
	// TODO:4字节对齐 
	#if 1
	if(Sz&0x0003){ // == Sz%4
		RealSz=4-(Sz&0x003);
	}
	#endif
	RealSz+=Sz;
	if(remain >= RealSz){
		ptmp=&g_StaticBuf_Space[g_StaticBuf_Cnt];
		g_StaticBuf_Cnt+=RealSz;
		
		g_StaticBuf_ReqCnt+=Sz;
	}
	else{
		// LOG_ERR();
		LLOG("#E SBUF OverFlow L[%d]\n",Line);
	}
	return ptmp;
}

void StaticBuf_Show(void) {	
	LLOG("#I SBUF: Use[%d] Remain[%d] Req[%d]\n",
	g_StaticBuf_Cnt,SBUF_MAX_SIZE-g_StaticBuf_Cnt,g_StaticBuf_ReqCnt);
}

#endif

#ifdef CFG_RANDOM_API
static double rnd1(double *r){   
    int m;
    double s,u,v,p;
    s=65536.0; // 随机数数值范围
    u=2053.0; 
    v=13849.0;

    m=(int)(*r/s);  //得出整数商
    *r=*r-m*s;      //得出余数
    //
    *r=u*(*r)+v;    //放大+偏移量
    m=(int)(*r/s);
    *r=*r-m*s;      //得出新余数
    p=*r/s;         //归一化	//输入的*r 是上一次产生的 随机数，未被归一化
    return(p);
}

int CC_Random(int seed,int range){ 		// 伪随机数
    int Rval;
    static double rand;
    double x;
    x=rnd1(&rand);
    Rval=(int)(x*range);
    return Rval;
}

#endif

#if 0 /*** ObjectArray SearchById Template */ 
#define TEMPLATE_GET_OBJECT_BYID(FUNCNAME,PTRTYPE,ARRAYNAME) \
PTRTYPE* FUNCNAME(unsigned char ID){\
    PTRTYPE* ptmp=PUB_NULL;\
    int ret=RET_ERR;\
    int i;\
	int ArraySz;\
	ArraySz=(sizeof(ARRAYNAME)/sizeof(ARRAYNAME[0]));\
    for(i=0;i<ArraySz;i++){\
        ptmp=(PTRTYPE*)&ARRAYNAME[i];\
        if(ptmp->Id == ID){\
            ret=RET_OK;\
            break;\
        }\
    }\
    if(ret == RET_ERR){\
        ptmp=PUB_NULL;\
    }\
    return ptmp;\
}
#endif

//

